package com.us.usws.site.console.action;

import java.io.File;

import org.apache.commons.lang3.StringUtils;

import com.us.usws.dto.ExtActionDTO;
import com.us.usws.dto.FileExtActionDTO;
import com.us.usws.support.uitls.JSONDataUtil;
import com.us.usws.support.uitls.ResourceUtil;

public class FileUploadAction extends BaseAction {

	private static final long serialVersionUID = -8812553671865666056L;

	private File file;

	private String fileFileName;
	
	private String fileContentType;

	private String dir;

	public void ajaxUploadFile() {
		getResponse().setContentType("text/html;charset=UTF-8");
		String path = getRealPath();
		path = ResourceUtil.uploadFile(path, dir, file, fileFileName);
		ExtActionDTO extActionDTO = new ExtActionDTO();
		if (StringUtils.isNotBlank(path)) {
			extActionDTO.setSuccess(true);
			extActionDTO.setResult(path);
		} else {
			extActionDTO.setSuccess(false);
			extActionDTO.setMsg("upload failure, please try again!");
		}
		getOut().print(JSONDataUtil.jsonStringfromObject(extActionDTO));
	}
	
	public void ajaxUploadVideoFile() {
		getResponse().setContentType("text/html;charset=UTF-8");
		String path = getRealPath();
		path = ResourceUtil.uploadFile(path, dir, file, fileFileName);
		FileExtActionDTO extActionDTO = new FileExtActionDTO();
		if (StringUtils.isNotBlank(path)) {
			extActionDTO.setSuccess(true);
			extActionDTO.setResult(path);
			extActionDTO.setFileName(fileFileName);
			extActionDTO.setFileSize(file.length());
			extActionDTO.setMimeType(fileContentType);
			int index = path.lastIndexOf(".");
			if(index != -1) {
				extActionDTO.setFileExt(path.substring(index+1));
			}
		} else {
			extActionDTO.setSuccess(false);
			extActionDTO.setMsg("upload failure, please try again!");
		}
		getOut().print(JSONDataUtil.jsonStringfromObject(extActionDTO));
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
}
